package cn.db101.kaoqinserver.controller;

import cn.db101.kaoqinserver.config.ApiResult;
import cn.db101.kaoqinserver.domain.Check;
import cn.db101.kaoqinserver.domain.User;
import cn.db101.kaoqinserver.model.request.ClassesParam;
import cn.db101.kaoqinserver.model.request.UserParam;
import cn.db101.kaoqinserver.service.UserService;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("v1/kaoqin/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("changeAvatar")
    public ApiResult<?> changeAvatar(@RequestBody User user) {

        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());
        user.setId(loginId);
        userService.updateUserAvatar(user);
        return ApiResult.ofSuccess();
    }

    @GetMapping("info")
    public ApiResult<?> info() {

        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());
        User userInfo = userService.getUserById(loginId);
        userInfo.setLoginPwd(null);
        return ApiResult.ofSuccess(userInfo);
    }

    @PostMapping("page")
    public ApiResult<?> page(@RequestBody UserParam userParam) {

        return ApiResult.ofSuccess(userService.userPage(userParam));
    }

    @PostMapping("create")
    public ApiResult<?> create(@RequestBody User user) {

        userService.create(user);
        return ApiResult.ofSuccess();
    }

    @PostMapping("checkStat")
    public ApiResult<?> checkStat(@RequestBody Check check) {

        return ApiResult.ofSuccess(userService.checkStat(check));
    }

    @GetMapping("logout")
    public ApiResult<?> logout() {

        StpUtil.logout();
        return ApiResult.ofSuccess();
    }

    @PostMapping("update")
    public ApiResult<?> update(@RequestBody User user) {

        userService.update(user);
        return ApiResult.ofSuccess();
    }

}
